package com.jstnvr.smartcampus.controller;


import com.jstnvr.smartcampus.entity.Users;
import com.jstnvr.smartcampus.service.UsersService;
import com.jstnvr.smartcampus.utils.GetUUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    /*
     * 用户处理
     * */

    @Autowired
    private UsersService usersService;

    @ResponseBody
    @GetMapping("/all")
    public Object findAllTest(
            @RequestParam(name = "pageNum", required = false, defaultValue = "1")
                    int pageNum,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10")
                    int pageSize) {
        return usersService.findAllUsers(pageNum, pageSize);
    }

    @RequestMapping("/login")

    public String login(HttpServletRequest request) {
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        System.out.print("测试用户名" + mobile + "|" + password);
        Users user = new Users();
        user.setMobile(mobile);
        user.setPassword(password);
        Users user2 = usersService.selectByMobile(mobile);
        if(null != user2)
        System.out.println("测试，根据电话查询处理的姓名：" + user2.getSzname());
        if (null != mobile && null != password) {
            Users exitsUsers = usersService.checkUser(mobile, password);
            if (null != exitsUsers) {
                //将用户登录信息放入到session中
                HttpSession session = request.getSession();
                session.setAttribute("szUid",exitsUsers.getSzuid());
                session.setAttribute("mobile", mobile);
                session.setAttribute("szName", exitsUsers.getSzname());
                /*这里先写死连两个用于测试，应从数据库中读取*/
                request.setAttribute("fileName", "12462210226106.jpg");
                request.setAttribute("fileName2","18173937439735.png");
                return "userFace";
            }
        }
        return "error";
    }

    @RequestMapping("/regist")
    @ResponseBody
    public String regist(HttpServletRequest request) {
        String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        if (null == mobile && null == password) {
            return "用户名或密码不能位空";
        }
        Users user = new Users();
        user.setMobile(mobile);
        user.setPassword(password);
        try {
            String szUid = GetUUID.getByUUId();
            user.setSzuid(szUid);
            System.out.println("user" + user.getSzuid() + "/" + user.getMobile() + "/" + user.getPassword());
            usersService.insertUsers(user);
            return "用户注册成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "用户注册失败";
    }
}
